<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Rpower;
use App\Admin\Rule;
use App\Admin\GetCategory;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class RulesController extends Controller
{

    //get Admin/role 全部友情列表
    public function index(Request $request)
    {
        $rules = Rule::all();
        $count = Rule::count();
        $page = 1;
        //2、设置每页显示条数
        $rev = '6';
//        //3、求总页数
        $sums = ceil($count/$rev);
//        //4、求当前页
//        $page = Input::get('page');
////        return $page;
//        if(empty($page)){
//            $page = "1";
//        }
//        $page = $page>0?$page:1;
//        $page = $page<$sums?$page:$sums;
//        //5、设置上一页、下一页
//        $prev = ($page-1)>0?$page-1:1;
//        $next = ($page+1)<$sums?$page+1:$sums;
//        //6、求偏移量
//        $offset = ($page-1)*$rev;
//        $rules = Rule::select()
//            ->offset($offset)
//            ->limit($rev)
//            ->get();
//        if($rules==[]){
//            $page = ($page-1)>0?$page-1:1;
//        }
        //dd($rules->toArray());
        $getCate = new GetCategory();
        //调用自定义的函数，生成树装的结构
        $rule = $getCate->getTree($rules);

        $rule = array_slice($rule, 0, 6);

        return view('admin/rule/rulelist',compact('rule','count','page','sums'));
    }
    public function changeOrder(Request $request)
    {
        $input = $request->all();
        $rule = Rule::find($input['id']);
        $rule->order = $input['order'];
        $res = $rule->save();
        if($res){
            $data = ['status'=>0,
                'msg'=>'权限排序更新成功',

            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'权限排序更新失败，请重试',
            ];
        }
        return $data;
    }

    public function changeSta(Request $request)
    {
        $input = $request->all();
        $user = Rule::find($input['id']);
        $user->is_menu=($user->is_menu==0?1:0);
        $res = $user->save();
        if($res){
            $data = ['status'=>0,
                'msg'=>'菜单更新成功',

            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'菜单更新失败，请重试',
            ];
        }
        return $data;
    }

    public function cherg(Request $request)
    {
        $input = Input::except('_token');
        $role_id = $input['role_id'];
        $rule_id = $input['id'];
        $res = Rpower::where('role_id',$role_id)->delete();
        $role=[];
        foreach($rule_id as $v){
            $rule[] = ['role_id'=>"$role_id",'rule_id'=>"$v"];
        }
        $res = DB::table('rpower')->insert($rule);
        if($res){
            return redirect('admin/user');
        }else{
            return back();
        }
    }
    //get Admin/rule/{rule} 显示单个分类信息
    public function show(Request $request)
    {

        $user = Rule::select()
            ->where('display_name','like','%'.$request->data.'%')
            ->get();
        //1、查询数据库总条数
        $count = count($user);
        //2、设置每页显示条数
        $rev = '6';
        //3、求总页数
        $sums = ceil($count/$rev);
        //4、求当前页
        $page = Input::get('page');
//        return $page;
        if(empty($page)){
            $page = "1";
        }
        $page = $page>0?$page:1;
        $page = $page<$sums?$page:$sums;
        //5、设置上一页、下一页
        $prev = ($page-1)>0?$page-1:1;
        $next = ($page+1)<$sums?$page+1:$sums;
        //6、求偏移量
        $offset = ($page-1)*$rev;
        $rules = Rule::
            where('display_name','like','%'.$request->data.'%')
            ->get()->toArray();
        //dump($rules);
        $getCate = new GetCategory();
        //调用自定义的函数，生成树装的结构
        $rule = $getCate->getTree($rules);
        $rule = array_slice($rule, $offset, $rev);
        //var_dump($rule);
        //die();
//        return $request->data;
        if($request->isMethod('post')) {
            $data='';
            if(!empty($rule)){
                foreach($rule as $u) {
                    if ($u['is_menu'] == 0) {
                        $str = "<td style='text-align:center;line-height:50px'><button class='btn btn-primary' id='btn" . $u['id'] . "' onclick='changesta(this," . $u['id'] . ")'>是</button></td>";
                    } else {
                        $str = "<td style='text-align:center;line-height:50px'><button class='btn btn-primary' id='btn" . $u['id'] . "' onclick='changesta(this," . $u['id'] . ")'>否</button></td>";
                    }
                    $data .=
                        "<tr><td style='line-height:50px'><input type='text' name='ord[]' value='"
                        . $u['order'] .
                        "'style='width:40px;text-align:center;line-height:50px;margin-bottom:0px' ></td><td style='text-align:center;line-height:50px'>"
                        . $u['id'] .
                        "</td><td style='text-align:center;line-height:50px'>"
                        .str_repeat('-', 4*$u['count']).$u['display_name'] .
                        " <td style='text-align:center;line-height:50px'>" . $u['name'] . "</td>" .
                        " <td style='text-align:center;line-height:50px'>".$u['description']."</td>".
                        "$str" .
                        "<td style='text-align:center;line-height:50px'>
                        <button type=\"button\" class='btn btn-danger'>
                    <a href=\"javascript:\" onclick=\"delArt(".$u['id'].")\" style=\"color:white\">删除</a>
                    </button>
                    " .
                        "</td>" .
                        "</tr>";
                }
                $a = "<a href='javascript:void(0)' onclick='page(1)'>首页</a><a href='javascript:void(0)' onclick='page(" . $prev . ")'>上一页</a><a href='javascript:void(0)' onclick='page(" . $next . ")'>下一页</a><a href='javascript:void(0)' onclick='page(" . $sums . ")'>尾页</a>";
                $arr = [
                    'data' => $data,
                    'page' => $a,
                    'count' => $count,
                    'ever' => $page,
                    'all' => $sums,
                    'msg' => '1',
                ];
                $json = json_encode($arr, JSON_UNESCAPED_UNICODE);
                return $json;
            }else{
                $data= "<th scope='col' style='text-align: center' colspan='7'>暂无用户</th>";
                $arr=['data'=>$data,'msg'=>0];
                $json = json_encode($arr,JSON_UNESCAPED_UNICODE);
                return $json;
            }
        }
        $data='';
        if(!empty($rule)) {
            foreach($rule as $u) {
                if ($u['is_menu'] == 0) {
                    $str = "<td style='text-align:center;line-height:50px'><button class='btn btn-primary' id='btn" . $u['id'] . "' onclick='changesta(this," . $u['id'] . ")'>是</button></td>";
                } else {
                    $str = "<td style='text-align:center;line-height:50px'><button class='btn btn-primary' id='btn" . $u['id'] . "' onclick='changesta(this," . $u['id'] . ")'>否</button></td>";
                }
                $data .=
                    "<tr><td style='line-height:50px'><input type='text' name='ord[]' value='"
                    . $u['order'] .
                    "'style='width:40px;text-align:center;line-height:50px;margin-bottom:0px' onchange='changeOrder(this,".$u['id'].")' ></td><td style='text-align:center;line-height:50px'>"
                    . $u['id'] .
                    "</td><td style='text-align:center;line-height:50px'>"
                    .str_repeat('-', 4*$u['count']).$u['display_name'] .
                    " <td style='text-align:center;line-height:50px'>".$u['name']."</td>".
                    " <td style='text-align:center;line-height:50px'>".$u['description']."</td>".
                    "$str" .
                    "<td style='text-align:center;line-height:50px'>
                    <button type=\"button\" class='btn btn-danger'>
                    <a href=\"javascript:\" onclick=\"delArt(".$u['id'].")\" style=\"color:white\">删除</a>
                    </button>
                    ".
                    "</td>".
                    "</tr>";

            }
            $a = "<a href='javascript:void(0)' onclick='page(1)'>首页</a><a href='javascript:void(0)' onclick='page(" . $prev . ")'>上一页</a><a href='javascript:void(0)' onclick='page(" . $next . ")'>下一页</a><a href='javascript:void(0)' onclick='page(" . $sums . ")'>尾页</a>";
            $arr = [
                'data' => $data,
                'page' => $a,
                'count' => $count,
                'ever' => $page,
                'all' => $sums,
                'msg' => '1',
            ];
            $json = json_encode($arr, JSON_UNESCAPED_UNICODE);
            return $json;

        }else{
            $data= "<th scope='col' style='text-align: center' colspan='7'>暂无用户</th>";
            $a = "<a href='javascript:void(0)' onclick='page(1)'>首页</a><a href='javascript:void(0)' onclick='page(1)'>上一页</a><a href='javascript:void(0)' onclick='page(1)'>下一页</a><a href='javascript:void(0)' onclick='page(1)'>尾页</a>";
            $arr=[
                'data'=>$data,
                'count'=>$count,
                'page'=>$a,

            ];
            $json = json_encode($arr,JSON_UNESCAPED_UNICODE);
            return $json;
        }
    }
    //get Admin/rule/create 添加权限
    public function create()
    {
        $display = Rule::where('pid', 0)->get();
        return view('admin/rule/ruleadd')->with('display', $display);
    }

    //post Admin/rule 添加更新权限
    public function store(Request $request)
    {
        $input = Input::except('_token');
        if($input){
            $rules = [
                'name'=>'required',
                'order'=>'numeric',
            ];
            $message = [
                'name.required'=>'权限不能为空!',
                'order.numeric'=>'排序必须是数字',
            ];
            $validator = Validator::make($request->all(),$rules,$message);
            if($validator->passes()){
                $res = Rule::create($input);
                if($res){
                    return redirect('admin/rule');
                }else{
                    return back()->with('errors','权限添加失败');
                }
            }else{
                return back()->withErrors($validator);
            }

        }
    }
    //delete Admin/rule/{rule}  删除自定义导航
    public function destroy($id)
    {
        $res = Rule::where('id',$id)->delete();
        if($res){
            $data = [
                'status'=>0,
                'msg'=>'权限删除成功!',
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'权限删除失败!',
            ];
        }
        return $data;
    }

}
